package com.freemalll.merchant.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.freemalll.merchant.domain.dto.merchantParameters.DTOMerParams;
import com.freemalll.merchant.domain.dto.merchantParameters.ExcelParsingDto;
import com.freemalll.merchant.domain.dto.merchantParameters.MerParamsDetailDTO;
import com.freemalll.merchant.domain.dto.merchantParameters.ParamQueryDTO;
import com.freemalll.merchant.domain.vo.merchantParameters.MerParamsDetailVO;
import com.freemalll.merchant.domain.vo.merchantParameters.MerParamsListVO;
import com.freemalll.merchant.service.MerParamsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 控制层。
 *
 * @author mybatis-flex-helper automatic generation
 * @since 1.0
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/merParams")
@Tag(name = "参数管理接口")
public class MerParamsController {

    private final MerParamsService merParamsService;


    /**
     * 分页查询参数信息
     */
    @PostMapping("/page")
    @Operation(summary = "分页查询参数列表")
    public Page<MerParamsListVO> page(@RequestBody ParamQueryDTO paramQueryDTO) {
        return merParamsService.paramsPageList(paramQueryDTO);
    }


    /**
     * 获取对应关键词的所有选项信息（包括子级）
     *
     * @return 详情
     */
    @GetMapping("/detail/{id}")
    @Operation(summary = "获取参数模板详情")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "参数id", required = true)
    public MerParamsDetailVO detail(@PathVariable String id) {
        return merParamsService.detail(id);
    }

    /**
     * 通过参数code获取参数详情
     *
     * @return 详情
     */
    @GetMapping("/cache/detail/{paramCode}")
    @Operation(summary = "获取参数详情(带缓存),用于其它页面查询")
    @Parameter(in = ParameterIn.PATH, name = "paramCode", description = "参数模版编码", required = true)
    public MerParamsDetailVO cacheDetail(@PathVariable String paramCode) {
        return merParamsService.cacheDetail(paramCode);
    }

    /**
     * 通过参数code和一级编码获取参数详情
     *
     * @return 详情
     */
    @GetMapping("/getParams")
    @Operation(summary = "获取参数详情,用于其它页面查询")
    public List<MerParamsDetailDTO> getParams(@Parameter(description = "参数编码", required = true)
                                                     @RequestParam(value = "paramCode") String paramCode,
                                              @Parameter(description = "一级编码", required = false)
                                                     @RequestParam(value = "firstLevelCoding", required = false) String firstLevelCoding) {
        return merParamsService.getParams(paramCode, firstLevelCoding);
    }


    /**
     * 添加或者修改
     *
     * @param merParams
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("/saveOrUpdate")
    @Operation(summary = "添加或更新")
    public String saveOrUpdate(@RequestBody @Valid DTOMerParams merParams) {
        return merParamsService.saveOrUpdate(merParams);
    }

    /**
     * 根据主键删除
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("/remove/{id}")
    @Operation(summary = "删除数据")
    public void remove(@PathVariable String id) {
        merParamsService.deleteById(id);
    }

    /**
     * 参数excel模板下载
     */
    @Operation(summary = "参数导入模板下载")
    @GetMapping(value = "/template/download")
    public void templateDownload() {
        merParamsService.templateDownload();
    }

    /**
     * 参数导出
     */
    @Operation(summary = "参数导出")
    @Parameter(in = ParameterIn.PATH, name = "paramId", description = "参数模板id", required = true)
    @GetMapping(value = "/export/{paramId}")
    public void exportParam(@PathVariable String paramId) {
        merParamsService.exportParam(paramId);
    }


    @Operation(summary = "参数导入")
    @PostMapping(value = "/import")
    public ExcelParsingDto importParam(@RequestParam("uploadFile") MultipartFile file) {
        return merParamsService.importParam(file);
    }


}
